using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Configuration;
using System.Diagnostics;
using System.Collections;
using System.Collections.Specialized;
using System.Threading;

namespace CLoggerNamespace
{
	public class CLogger
	{
		private static int _ALL = 100;
		private static int _INFO = 75;
		private static int _ERROR = 50;
		private static int _OFF = 0;
		private static int _COMM = 40;
        private static String strLogDirectory = "C:\\Fabio\\Proyectos\\Hs\\hsdll\\Log\\";

		private static String strLogPrefix = "hsdll";

		public static string LogPrefix
		{
			set { strLogPrefix = value; }
		}

		public static int ALL
		{
			get { return _ALL; }
		}
		public static int ERROR
		{
			get { return _ERROR; }
		}
		public static int INFO
		{
			get { return _INFO; }
		}
		public static int OFF
		{
			get { return _OFF; }
		}
		public static int COMM
		{
			get { return _COMM; }
		}

		

		private static string LogLevelToString(int level)
		{
			string strLogLevel;
			switch (level)
			{
				case 100: // _ALL
					strLogLevel = "ALL  ";
					break;
				case 50: // _ERROR
					strLogLevel = "ERROR";
					break;
				case 75: // _INFO
					strLogLevel = "INFO ";
					break;
				case 40: // _COMM
					strLogLevel = "COMM ";
					break;
				case 0: // _OFF
				default:
					strLogLevel = "OFF  ";
					break;
			}
			return strLogLevel;
		}

		public static void Append(String message, int level)
		{

			StackTrace stackTrace;
			String strMethodName;
			int logLevel = _OFF;


			stackTrace = new System.Diagnostics.StackTrace();
			strMethodName = stackTrace.GetFrame(1).GetMethod().Name;
          
                    try
                    {
                        DateTime dt = DateTime.Now;
                        String filePath = strLogDirectory + "\\" + strLogPrefix + dt.ToString("yyyyMMdd") + ".log";
                        if (!File.Exists(filePath))
                        {
                            FileStream fs = File.Create(filePath);
                            fs.Close();
                        }

                        try
                        {
                            StreamWriter sw = File.AppendText(filePath);
                            sw.WriteLine(dt.ToString("HH:mm:ss") + "|" + "Thread ID: " + Thread.CurrentThread.ManagedThreadId.ToString() + "|" + LogLevelToString(level) + "|" + strMethodName + "|" + message);
                            sw.Flush();
                            sw.Close();
                            sw.Dispose();
                            sw = null;
                        }
                        catch ( Exception ex ) 
                        {
                           //	Console.WriteLine(ex.Message.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                       // Console.WriteLine(ex.Message.ToString());
                    }
                			
		}
	}
}
